package _07_串;

public class _859_亲密字符串 {

    public static void main(String[] args) {

        _859_亲密字符串 v = new _859_亲密字符串();

        String s = "ab";
        String s1 = "ba";
        System.out.println(v.buddyStrings(s, s1));

    }

    public boolean buddyStrings(String s, String goal) {
        int sLen = s.length();
        // 如果字符串长度不相等，直接返回
        if (s.length() != goal.length()) {
            return false;
        }

        if (s.equals(goal)) {
            // 字符串相等，相同数字大于1至为true
            int[] ins = new int[26];
            for (int i = 0; i < sLen; i++) {
                if (++ins[s.charAt(i) - 'a'] > 1) {
                    return true;
                }
            }
            return false;
        }
        int first = -1, second = -1;
        for (int i = 0; i < sLen; i++) {
            if (s.charAt(i) != goal.charAt(i)) {
                if (first == -1) {
                    first = i;
                } else if (second == -1) {
                    second = i;
                } else {
                    return false;
                }
            }
        }
        return second != -1 && s.charAt(first) == goal.charAt(second) && s.charAt(second) == goal.charAt(first);
    }


}








